# -*- coding:utf-8 -*-
# @FileName  :shenyibao_t1.py
# @Time      :2021/6/19 9:49
import io
import re
import time

import pandas as pd

from save_mysql_shenyibao import mysql_checking
import requests
from lxml import html
from sql_ac.sql_1 import insert_one, select_all, insert_all
import sys
from fake_useragent import UserAgent

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030')
sys.setrecursionlimit(1000000)
etree = html.etree

ua = UserAgent()
kv = {
    'user-agent': ua.random,
    "Connection": 'close',
    # 'cookie':'BIDUPSID=1D1BB89E4109822092F63538BF212A0B; PSTM=1618373808; BAIDUID=1D1BB89E41098220F0AAE19A13D15EE2:FG=1; __yjs_duid=1_8d6fb1349911283e51e25afb4c5675de1618373866311; BDUSS=zZtVC1sNUUxMzNBVkZFR1dpblEtOUdKOTkyQTdqOVBVM1dIMUxvUk5HM052NjFnSUFBQUFBJCQAAAAAAAAAAAEAAAD2vuuauf65~sqlttwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM0yhmDNMoZgc; BDUSS_BFESS=zZtVC1sNUUxMzNBVkZFR1dpblEtOUdKOTkyQTdqOVBVM1dIMUxvUk5HM052NjFnSUFBQUFBJCQAAAAAAAAAAAEAAAD2vuuauf65~sqlttwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM0yhmDNMoZgc; B2B_AB_FCT2=42511786; H_WISE_SIDS=107313_110085_127969_128699_154619_165936_171509_171565_172643_173016_173184_173293_173617_174179_174447_174662_174665_174682_175407_175535_175553_175668_175678_175755_175796_176130_176158_176328_176342_176346_176487_176615_176678_176766_176902_176996_177086_177224_177280_177381_177407_177558; B2B_CL_HISTORY_WL=%5B%7B%22_q%22%3A%22%5Cu6d0b%5Cu9152%22%2C%22_c%22%3A%22%5Cu98df%5Cu54c1%5Cu751f%5Cu9c9c%3B%5Cu9152%5Cu7c7b%3B%5Cu6d0b%5Cu9152%22%2C%22_k%22%3A%2248b9d7bf794f3fd0382dc977c651eef0%22%2C%22_t%22%3A1623378623%7D%5D; MCITY=-340%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; B2B_AB_SID=201_260_263; BAIDUID_BFESS=1D1BB89E41098220F0AAE19A13D15EE2:FG=1; Hm_lvt_77fe88a330b395c39e37e1ea1cea9a8c=1623485790,1623728020,1623895341,1623897184; B2B_SE_HISTORY_WL=%5B%7B%22_w%22%3A%22%5Cu5973%5Cu88c5%22%2C%22_t%22%3A1623897663%7D%2C%7B%22_w%22%3A%22%5Cu94c5%5Cu7b14%22%2C%22_t%22%3A1623741613%7D%2C%7B%22_w%22%3A%22%5Cu822a%5Cu661f%5Cu6d17%5Cu6da4%5Cu673a%5Cu68b0%28%5Cu6cf0%5Cu5dde%29%5Cu6709%5Cu9650%5Cu516c%5Cu53f8%22%2C%22_t%22%3A1623739944%7D%2C%7B%22_w%22%3A%22%5Cu6d17%5Cu8863%5Cu673a%22%2C%22_t%22%3A1623739870%7D%2C%7B%22_w%22%3A%22%5Cu6f4d%5Cu574a%5Cu8fc5%5Cu641c%5Cu7f51%5Cu7edc%5Cu6280%5Cu672f%5Cu6709%5Cu9650%5Cu516c%5Cu53f8%22%2C%22_t%22%3A1623739648%7D%2C%7B%22_w%22%3A%22%5Cu8033%5Cu673a%22%2C%22_t%22%3A1623728017%7D%2C%7B%22_w%22%3A%22%5Cu5316%5Cu5986%5Cu54c1%22%2C%22_t%22%3A1623489641%7D%2C%7B%22_w%22%3A%22%5Cu5361%5Cu76d2%22%2C%22_t%22%3A1623489430%7D%2C%7B%22_w%22%3A%22%5Cu7259%5Cu818f%22%2C%22_t%22%3A1623487400%7D%2C%7B%22_w%22%3A%22%5Cu6d0b%5Cu9152%22%2C%22_t%22%3A1623378619%7D%5D; H_PS_PSSID=34099_33968_33848_33607_26350; delPer=0; PSINO=6; BA_HECTOR=a021242420a42g8hrq1gcle1t0q; Hm_lpvt_77fe88a330b395c39e37e1ea1cea9a8c=1623898223; ab_sr=1.0.1_N2IwZGU3MTEyYTczYjhiYzQ0YzFmYzI5NjhhMTE5YjJhYWMxZjNhNmM2ZWI4M2YwZTYyYTI5YzI2NzY2MDVjNjMyMmZmM2FkMmM1N2NiOWIyYjc1OTk0Y2Q0MzFjMTZhNjBkYzczZTJhNGNlZjUyM2ExYTUwMGVhNmQxMTQ3NTZjMDAxMjlkYjJiZTliODg1OGQzODUzYWNmZmZiNDk5OA=='
}


def pp(e):
    if len(e) != 0:
        return e[0]
    else:
        return ''


def main(key,page):
    # print('http://cn.toocle.com/search/index.php?f=main&terms=' + key + '&type=1&p=2.html')
    req = requests.get('http://cn.toocle.com/search/index.php?f=main&terms=' + key + '&type=1&p='+str(page)+'.html',
                       headers=kv).text
    # print(req)
    sou = etree.HTML(req)
    url = sou.xpath("//div[@class='row']//div[@class='product-info']/h3/a/@href")

    for i in url:
        req1 = requests.get(i + 'company.html', headers=kv).text
        sou1 = etree.HTML(req1)
        # print(req1)
        compname = pp(sou1.xpath(
            "//div[@class='description-subcontent']/ul/li[1]/div[@class='col-9 info-section-intro']/p/text()"))
        name = pp(sou1.xpath(
            "//div[@class='description-subcontent']/ul/li[2]/div[@class='col-9 info-section-intro']/p/text()"))
        telephone = pp(sou1.xpath(
            "//div[@class='description-subcontent']/ul/li[3]/div[@class='col-9 info-section-intro']/p/text()"))
        phone = pp(sou1.xpath(
            "//div[@class='description-subcontent']/ul/li[4]/div[@class='col-9 info-section-intro']/p/text()"))
        addr = pp(sou1.xpath(
            "//div[@class='description-subcontent']/ul/li[8]/div[@class='col-9 info-section-intro']/p/text()"))
        hx = pp(sou1.xpath(
            "//div[@class='description-subcontent']/ul/li[8]/div[@class='col-12 info-section-intro']/p/text()"))
        ess = pp(sou1.xpath("//ul[@class='syb-shop_contacttype-info-ul']/li[5]/a/text()"))
        if len(phone.replace('-', '')) != 11 or phone == '':
            continue
        print(compname)
        print(name)
        print(telephone)
        print(phone)
        print(addr)
        print(hx)
        print('============' * 10)
        if mysql_checking(phone.replace('-', '')):
            insert_all('shenyibao_spider', name.replace("'", "").replace("'", ""), telephone,
                       phone.replace('-', ''), key,
                       compname, '', 13,
                       '0', 1, '', '', '', '', '', '', 2, '', '',
                       (addr + ess).replace("'", "").replace("'", ""), '', '', '', '', '', '', 0, '',
                       0, '', 0, 0,
                       2, 1, 0, str(time.strftime('%Y-%m-%d %H:%M:%S')), str(time.strftime('%Y-%m-%d %H:%M:%S')), hx,
                       0,
                       0, '', 0)
            # input()


def url_list():
    df = pd.read_excel(r'C:\Users\Administrator\PycharmProjects\test_1\aicaigou\阿里巴巴关键词.xls', usecols=[0], names=None)
    # 读取项目名称列,不要列名
    df_li = df.values.tolist()
    result = []
    for s_li in df_li:
        result.append(s_li[0])
    return result


if __name__ == "__main__":
    v = 0
    for i in url_list():
        if i == '鱼饵':
            continue
        req = requests.get('http://cn.toocle.com/search/?f=main&type=1&terms=' + i, headers=kv).text
        sou = etree.HTML(req)
        page = sou.xpath("//div[@class='megs']/text()")
        if page[2].replace("的信息，共", "").replace("页", "") == '':
            print('没有数据', i)
        else:
            # v += int(page[2].replace("的信息，共", "").replace("页", ""))
            # print(page[2].replace("的信息，共", "").replace("页", ""), i)
        # print(v)
            for j in range(1,int(page[2].replace("的信息，共", "").replace("页", ""))+1):
                main(i,j)
